import { useEffect } from 'react'

import {
  createCompanionContentApi,
  getCompanionContentApi,
  updateCompanionContentApi,
} from '@/api/pcs/pcsCompanionContent'
import AekForm from '@/components/AekForm'
import AekModal, { PromiseThen } from '@/components/AekModal'
import {
  PCS_CATHETERCOMPANION_ENUM,
  PCS_CONTENT_AVAILABLEPOPULATION_ENUM,
  PCS_EATINGMETHOD_ENUM,
  PCS_MEDICALSYMPTOMS_ENUM,
  PCS_MEDICATIONASSISTANCE_ENUM,
  PCS_OTHERCOMPANIONSHIP_ENUM,
  PCS_SELFCARESTATUS_ENUM,
} from '@/utils/enum'
import { requestWithTry } from '@/utils/request'

const EditModal = ({ row = {} }) => {
  const [form] = AekForm.useForm()
  const availablePopulation = AekForm.useWatch('availablePopulation', form)
  const { id } = row

  const getDetail = async () => {
    const { data } = await requestWithTry(getCompanionContentApi, {
      id,
    })
    form.setFieldsValue({
      ...data,
    })
  }

  useEffect(() => {
    form.setFieldsValue({
      availablePopulation: PCS_CONTENT_AVAILABLEPOPULATION_ENUM.ALL,
    })
    if (!id) return
    getDetail()
  }, [id])

  /** @type {import("@/components/AekForm").FormDataProps[]} */
  const formData = [
    {
      field: 'content',
      label: '陪护内容',
      formItemProps: {
        rules: [{ required: true }],
      },
      component: { name: 'Input', props: { maxLength: 150 } },
    },
    {
      field: 'availablePopulation',
      label: '适用对象',
      formItemProps: {
        rules: [{ required: true }],
      },
      component: {
        name: 'RadioGroup',
        props: {
          options: PCS_CONTENT_AVAILABLEPOPULATION_ENUM.options,
        },
      },
    },
    {
      field: 'announcement',
      label: '注意事项',
      component: { name: 'AekTextArea' },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'selfCareStatus',
      label: '自理情况',
      component: {
        name: 'RadioGroup',
        props: {
          options: PCS_SELFCARESTATUS_ENUM.options,
        },
      },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'medicalSymptoms',
      label: '病情症状',
      component: {
        name: 'CheckboxGroup',
        props: {
          options: PCS_MEDICALSYMPTOMS_ENUM.options,
        },
      },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'eatingMethod',
      label: '进食方式',
      component: {
        name: 'RadioGroup',
        props: {
          options: PCS_EATINGMETHOD_ENUM.options,
        },
      },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'catheterCompanions',
      label: '导管陪护',
      component: {
        name: 'CheckboxGroup',
        props: {
          options: PCS_CATHETERCOMPANION_ENUM.options,
        },
      },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'medicationAssistance',
      label: '药物协助',
      component: {
        name: 'CheckboxGroup',
        props: {
          options: PCS_MEDICATIONASSISTANCE_ENUM.options,
        },
      },
    },
    {
      exclude: availablePopulation !== PCS_CONTENT_AVAILABLEPOPULATION_ENUM.RESTRICTED,
      field: 'otherCompanionship',
      label: '其他陪护',
      component: {
        name: 'CheckboxGroup',
        props: {
          options: PCS_OTHERCOMPANIONSHIP_ENUM.options,
        },
      },
    },
  ]

  const onOk = async () => {
    const values = await form.validateFields()
    const params = {
      id,
      ...values,
    }
    await requestWithTry(id ? updateCompanionContentApi : createCompanionContentApi, params)
  }
  return (
    <AekModal title={id ? '编辑' : '新增'} width={500} onOk={onOk}>
      <AekForm form={form} formData={formData} />
    </AekModal>
  )
}

EditModal.PromiseThen = PromiseThen

export default EditModal
